<?php
namespace App\Http\Controllers\admin_system;
use DB;
use Illuminate\Http\Request;


class AppVersionController{


    //已发布的版本列表
    public function versions()
    {
        $apps = DB::table('app_version')->orderBy('id','desc')->paginate(20);
        foreach($apps as $p){
            $content    = str_replace(array("\r\n", "\r", "\n"), "", $p->content);
            $p->content = explode(';',$content);
        }
        return view('admin_system/appversion/versions',['apps'=>$apps]);
    }



    public function edit(Request $request)
    {
        $id     = $request->input('id',0);
        if($id == 0)
        {
            $app    = new \stdClass();
            $app->id            = 0;
            $app->version_name  = "";
            $app->version_code  = "";
            $app->content       = "";
            $app->url           = "";
            $app->client        = "Android";
            $app->is_show       = 0;
        }
        else
        {
            $app = DB::table('app_version')->where('id',$id)->first();
        }

        return view('admin_system/appversion/edit',['app'=>$app]);
    }



    public function edit_action(Request $request)
    {
        $appInfo    = $request->except('apk','_token');
        $id         = $request->id;
        $content    = $appInfo['content'];
        $content    = str_replace('；',';',$content);
        $appInfo['content']     = trim($content,';');
        $appInfo['updated_at']  = date_time();


        if($request->file('apk')){
            $file       = $request->file('apk');
            $fileName   = date('ymdHms',time()).".".$file->getClientOriginalExtension();
            $path       = public_path('apk');
            $info       = $file->move($path,$fileName);
            $appInfo['apk_url'] = "apk/".$fileName;
            $oldfile    = $path."/".$fileName;
            copy($oldfile,$path.'/Reading4.apk');
        }

        $db = DB::table('app_version');
        if($id > 0)
        {
            $db->where('id',$id)->update($appInfo);
        }
        else
        {
            $appInfo['created_at']  = date_time();
            $db->insert($appInfo);
        }

        return 'ok';
        //return redirect('admin_system/appversion/versions');

    }



}